package com.example.contextcube.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;

import androidx.preference.PreferenceManager;

/**
 * 字体大小管理工具类
 */
public class FontSizeManager {
    private static final String KEY_FONT_SIZE = "font_size";
    private static final String SIZE_SMALL = "small";
    private static final String SIZE_MEDIUM = "medium"; // 默认
    private static final String SIZE_LARGE = "large";
    private static final String SIZE_XLARGE = "xlarge";

    // 字体缩放因子
    private static final float SCALE_SMALL = 0.85f;
    private static final float SCALE_MEDIUM = 1.0f;
    private static final float SCALE_LARGE = 1.15f;
    private static final float SCALE_XLARGE = 1.3f;

    private static FontSizeManager instance;

    private FontSizeManager() {
        // 私有构造函数
    }

    /**
     * 获取单例实例
     */
    public static synchronized FontSizeManager getInstance() {
        if (instance == null) {
            instance = new FontSizeManager();
        }
        return instance;
    }

    /**
     * 获取当前字体大小设置
     * @param context 上下文
     * @return 字体大小设置值
     */
    public String getCurrentFontSize(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getString(KEY_FONT_SIZE, SIZE_MEDIUM);
    }

    /**
     * 应用字体大小设置
     * @param context 上下文
     * @param size 字体大小设置值
     */
    public void applyFontSize(Context context, String size) {
        float scale = getFontScale(size);
        updateConfiguration(context, scale);
    }

    /**
     * 应用字体大小到上下文
     * @param context 上下文
     * @return 新的配置上下文
     */
    public Context createConfigurationContext(Context context) {
        String size = getCurrentFontSize(context);
        float scale = getFontScale(size);
        
        Configuration configuration = new Configuration(context.getResources().getConfiguration());
        configuration.fontScale = scale;
        
        return context.createConfigurationContext(configuration);
    }

    /**
     * 获取字体缩放因子
     * @param size 字体大小设置值
     * @return 缩放因子
     */
    public float getFontScale(String size) {
        switch (size) {
            case SIZE_SMALL:
                return SCALE_SMALL;
            case SIZE_LARGE:
                return SCALE_LARGE;
            case SIZE_XLARGE:
                return SCALE_XLARGE;
            case SIZE_MEDIUM:
            default:
                return SCALE_MEDIUM;
        }
    }

    /**
     * 更新配置
     * @param context 上下文
     * @param fontScale 字体缩放因子
     */
    private void updateConfiguration(Context context, float fontScale) {
        Context appContext = context.getApplicationContext();
        Resources resources = appContext.getResources();
        Configuration configuration = new Configuration(resources.getConfiguration());
        configuration.fontScale = fontScale;

        // 更新应用配置
        DisplayMetrics metrics = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, metrics);
    }
} 